home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / pc / MCASM.RAR / MC_ASM.EXE / WROX_ASM / CH12 / FORMATS / FORMATS.H < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-18  |  8.1 KB  |  303 lines

  1. // This header contains stuctures and classes
  2. // of graphics file formats readers and savers
  3.  
  4. #ifndef FORMATS_H
  5. #define FORMATS_H
  6.  
  7. #include "grfile.h"
  8. #include "graph.h"
  9.  
  10. //----------------------------------- TARGA ----------------------------
  11.  
  12. typedef struct {    // targa file header
  13.     BYTE ID_field_length;
  14.     char color_map_type;
  15.     BYTE image_type;
  16.  
  17.     WORD first_colmap_entry;
  18.     WORD colmap_length;
  19.     char colmap_entry_size;
  20.  
  21.     int x;
  22.     int y;
  23.     int width;
  24.     int height;
  25.     char bitsperpixel;
  26.     BYTE attrbits : 4;
  27.     BYTE right_to_left : 1;
  28.     BYTE top_to_bottom : 1;
  29.     BYTE interleave : 2;
  30.     } targa_header;
  31.  
  32. typedef colortype targa_palette[256];
  33.  
  34. class targa_file: public graph_file_reader { // TARGA file reader
  35. private:
  36.     targa_header header;
  37.     int bytesperpixel,counter;
  38.     BOOL rle_comp;
  39. public:
  40.     targa_file(char *fname);     // open file, read headers and palette
  41.                     // allocates source_line
  42.     virtual ~targa_file();
  43.     virtual int get_next_line();    // returns -1 if file is finished
  44. };
  45.  
  46. class targa_file_saver: public graph_file_saver { // TARGA file saver
  47. private:
  48.     targa_header header;
  49.     int bytesperpixel,counter,count_row;
  50.     BOOL rle_comp;
  51.     int *arr_ofs;
  52. public:
  53.     targa_file_saver(char *fname, image_type dest_type_,
  54.             image_extention_type dest_ext_type_,
  55.             int width_,int height_,BGRpalette* pal,BOOL compress,
  56.             BOOL top_to_bottom_,BOOL left_to_right_);
  57.     virtual void update_palette(BGRpalette *pal);
  58.     virtual int put_next_line(BYTE* line);    // returns -1 if file is finished
  59.     ~targa_file_saver();
  60. };
  61.  
  62. //----------------------------------- BMP ------------------------------
  63.  
  64. const WORD BMPSIGNATURE = 'M'*256+'B';
  65.  
  66. typedef struct {
  67.     WORD bf_type;         //signature
  68.     long bf_size;          //size of file in DWORD
  69.     WORD bf_reserved1, bf_reserved2;
  70.     long bf_offbits;    //image start offset in byte
  71. } bmp_file_header;
  72.  
  73. typedef struct {
  74.     long bi_size;
  75.     long bi_width;
  76.     long bi_height;
  77.     WORD bi_planes;
  78.     WORD bi_bit_count;
  79.     long bi_compression;
  80.     long bi_size_image;
  81.     long bi_xpels_per_meter;
  82.     long bi_ypels_per_meter;
  83.     long bi_clr_used;
  84.     long bi_clr_important;
  85. } bmp_file_info_header;
  86.  
  87.  
  88. class bmp_file: public graph_file_reader  { // bitmap file reader
  89. private:
  90.     bmp_file_header header;
  91.     bmp_file_info_header info;
  92. public:
  93.     bmp_file(char *fname); //open file, check signature, read headers and palette
  94.                    //allocates source_line
  95.     virtual ~bmp_file();
  96.     virtual int get_next_line();    //returns -1 if file is finished
  97. };
  98.  
  99.  
  100. void build_bmp_info(bmp_file_info_header &info,
  101.         image_type f_type,    long width,long height);
  102.  
  103.  
  104. class bmp_file_saver: public  graph_file_saver{
  105.     bmp_file_info_header info;
  106.     void init(bmp_file_info_header *info_);
  107.         // is called from constructors
  108. public:
  109.     bmp_file_saver(char *fname,image_type dest_type_,
  110.              BGRpalette *palette, bmp_file_info_header *info_);
  111.     bmp_file_saver(char *fname,image_type dest_type_,
  112.             int width_,int height_,BGRpalette * pal);
  113.     virtual void update_palette(BGRpalette *pal);
  114.     virtual int put_next_line(BYTE *line);    //returns -1 if some error occurs
  115. };
  116.  
  117. //----------------------------------- GIF ------------------------------
  118.  
  119. #define LZW_BITS    12
  120. #define LZW_TABLE_SIZE    0x1000    // 1 << LZW_BITS
  121. #define HASH_SIZE    5003
  122.  
  123. const char GIFSIGNATURE[3] = "GIF";
  124.  
  125. typedef struct {
  126.     char signature[3];        // = "GIF"
  127.     char version[3];        // = "87a" or "89a" (version)
  128.     } gif_header;
  129.  
  130. typedef struct {
  131.     int width;
  132.     int height;
  133.     BYTE sizeofGCT : 3;        // size of Global Color Table
  134.     BYTE sortflag : 1;        // sort flag
  135.     BYTE colorres : 3;        // color resolution
  136.     BYTE GCTflag : 1;        // Global Color Table flag
  137.     BYTE backcolor;            // background color
  138.     BYTE pixelaspectratio;        // pixel aspect ratio
  139.     } logical_screen_descriptor;
  140.  
  141. #define IMAGE_DESCRIPTOR 0x2C
  142.  
  143. typedef struct {
  144.     int x;                // image left position
  145.     int y;                // image top position
  146.     int width;
  147.     int height;
  148.     BYTE sizeofLCT : 3;
  149.     BYTE reserved : 2;
  150.     BYTE sortflag : 1;        // sort flag
  151.     BYTE interlaceflag : 1;        // interlace flag
  152.     BYTE LCTflag : 1;        // Local Color Table flag
  153.     } image_descriptor;
  154.  
  155. class gif_file: public graph_file_reader { // GIF file reader
  156. private:
  157.     gif_header header;
  158.     logical_screen_descriptor scrdesc;
  159.     image_descriptor imagedesc;
  160.     WORD *prefixcode;
  161.     BYTE *secondbyte;
  162.     BYTE *decodestack;
  163.     BYTE LZW_code_size;
  164.     int num_bits,clear_code,end_of_info,first_code,max_code;
  165.  
  166.     DWORD bit_buffer;
  167.     int bit_count,counter;
  168.     BYTE blocksize,pixelvalue;
  169.     BOOL clear_flag;
  170.     WORD next_code,new_code,old_code;
  171.     BYTE *stack_top;
  172.  
  173.     WORD getcode();
  174.     BYTE *decode(BYTE *stack_bottom, WORD code);
  175.     // gif uses papa's buffer in specifical way, so
  176.     // it allocates and disposes it in constructors and destructors
  177. public:
  178.     gif_file(char *fname);         // open file, check signature, read headers and palette
  179.                     // allocates source_line
  180.     virtual ~gif_file();
  181.     virtual void seek_start();     // seek to image itself start (bottom line)
  182.     virtual int get_next_line();    // returns -1 if file is finished
  183. };
  184.  
  185. class gif_file_saver: public graph_file_saver { // GIF file saver
  186. private:
  187.     gif_header header;
  188.     logical_screen_descriptor scrdesc;
  189.     image_descriptor imagedesc;
  190.  
  191.     int *code_value;
  192.     WORD *prefixcode;
  193.     BYTE *secondbyte;
  194.     BYTE LZW_code_size;
  195.     int num_bits,clear_code,end_of_info,first_code,max_code;
  196.  
  197.     DWORD bit_buffer;
  198.     int bit_count;
  199.     WORD next_code,code;
  200.     BOOL first_byte;
  201.  
  202.     void putcode(WORD code);
  203.     int find_match(int hash_prefix,WORD hash_symbol);
  204.     // gif uses papa's buffer in specifical way, so
  205.     // it allocates and disposes it in constructors and destructors
  206. public:
  207.     gif_file_saver(char *fname, image_type dest_type_,
  208.             int width_,int height_,BGRpalette* pal);
  209.                 // open file, write signature, write headers and palette
  210.     virtual void update_palette(BGRpalette *pal);
  211.     virtual ~gif_file_saver();
  212.     virtual int put_next_line(BYTE *line);
  213. };
  214.  
  215.  
  216.  
  217. const char PCXSIGNATURE = 0x0A;
  218.  
  219. typedef struct {
  220.     char signature;
  221.     char version;
  222.     char encoding;
  223.     char bitsperpixel;
  224.     int x1;
  225.     int y1;
  226.     int x2;
  227.     int y2;
  228.     int Xres,Yres;
  229.     colortype palette[16];
  230.     char reserved;
  231.     char planes;
  232.     int bytesperline;
  233.     int palinterp;
  234.     int videoscrsizex;
  235.     int videoscrsizey;
  236.     char res[54];
  237.     } pcx_header;
  238.  
  239. class pcx_file: public graph_file_reader { // PCX file reader
  240. private:
  241.     pcx_header header;
  242.     BYTE prev_c,rep,datab;
  243.     int real_len;
  244. public:
  245.     pcx_file(char *fname);         // open file, check signature, read headers and palette
  246.                     // allocates source_line
  247.     virtual ~pcx_file();
  248.     virtual void seek_start();     // seek to image itself start (bottom line)
  249.     virtual int get_next_line();    // returns -1 if file is finished
  250.     long image_size();        // returns estimation of memory required to store image
  251. };
  252.  
  253. class pcx_file_saver: public graph_file_saver { // PCX file saver
  254. private:
  255.     pcx_header header;
  256. public:
  257.     pcx_file_saver(char *fname, image_type dest_type_,
  258.             image_extention_type dest_ext_type_,
  259.             int width_,int height_,BGRpalette* pal);    // open file, write headers
  260.                     // allocates source_line
  261.     virtual void update_palette(BGRpalette *pal);
  262.     virtual int put_next_line(BYTE *line);
  263. };
  264.  
  265. /*------------------*/
  266. /* any file opening */
  267.  
  268. graph_file_reader *open_image_file(char *fname);
  269. // if error occured returns null
  270. // and sets format_file_error in non-zero value
  271.  
  272. graph_file_saver *open_saver_file(char *fname,
  273.         image_type dest_type,
  274.         image_extention_type dest_ext_type,
  275.         int width, int height,
  276.         BGRpalette* palptr,
  277.         BOOL top_to_bottom, // if possible
  278.         BOOL left_to_right);
  279. // if error occured returns null
  280. // and sets format_file_error in non-zero value
  281.  
  282. extern int format_file_error;
  283. // 0 - Ok
  284. // 1 - File not found
  285. // 2 - Unknown file format
  286. // 3 - Format header error
  287. // 4 - Disk read / write error
  288.  
  289. char *format_file_error_report();
  290.  
  291. void choosevideomode(graph_file_reader *grf_r);
  292.     // set optimal (but 256 color) mode for image
  293.  
  294. void setpalette_by_BGR(BGRpalette pal);
  295.     // actually sets palette with values pal[i].x/4
  296.  
  297. // init simple standard palette for 256 colors mode
  298. void stand_palette(VGApalette &pal);
  299.  
  300. // simplest way to preview truecolor
  301. void put_TC_row(BYTE *rp, BYTE *gp, BYTE *bp,int x,int y,int xcount);
  302.  
  303. #endif